package com.htj.admin.controller;

import cn.hutool.core.util.IdUtil;
import com.htj.admin.dao.OrderDetailDao;
import com.htj.admin.domain.*;
import com.htj.admin.service.*;
import com.htj.common.Result;
import com.htj.util.JWTUtil;
import com.htj.vo.PageListVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "order")
@CrossOrigin
public class OrderController {
    @Autowired
    OrderServiceInf orderServiceInf;
    @Autowired
    FrontUserServiceInf frontUserServiceInf;
    @Autowired
    OrderDetailDao orderDetailDao;
    @Autowired
    CartServiceInf cartServiceInf;
    /**
     * 新增订单
     * @param
     * @return
     */
    @PostMapping("/save")
    public Result<String> add(@RequestBody Order order,@RequestHeader String token){
        token = token.substring(1).substring(0,token.length() - 2);
        String id = JWTUtil.verify(token).getClaim("id").asString();
        FrontUser frontUser = frontUserServiceInf.getUserById(id);
        order.setUserId(String.valueOf(frontUser.getId()));
        order.setUserName(frontUser.getName());
        boolean bool = orderServiceInf.add(order);
        return bool?Result.succ():Result.fail();
    }

    /**
     * 分页数据
     * @param currentPage
     * @param pageSize
     * @param
     * @return
     */
    @GetMapping("/pageList")
    public Result<PageListVo> pageList(@RequestParam String currentPage,
                                       @RequestParam String pageSize,
                                       @RequestParam String userName){
        HashMap<String, String> map = new HashMap<>();
        map.put("currentPage",currentPage);
        map.put("pageSize",pageSize);
        map.put("userName",userName);
        PageListVo<Order> pageList = orderServiceInf.pageList(map);
        return Result.succ(pageList);
    }

    /**
     * 删除订单
     * @param id
     * @return
     */
    @DeleteMapping("delete/{id}")
    public Result<String> delete(@PathVariable Integer id){
        boolean bool = orderServiceInf.delete(id);
        return bool?Result.succ():Result.fail();
    }

    /**
     * 改变订单状态
     * @param map
     * @return
     */
    @PostMapping("/changeStatus")
    public Result<String> changeStatus(@RequestBody Map<String,String> map){
        boolean bool = orderServiceInf.changeStatus(map);
        return bool?Result.succ():Result.fail();
    }

    @PostMapping("/changePay")
    public Result<String> changePay(@RequestBody Map<String,String> map){
        boolean bool = orderServiceInf.changePay(map);
        return bool?Result.succ():Result.fail();
    }

    @GetMapping("/detail")
    public Result<List<OrderDetail>> detail(@RequestParam String id){
        List<OrderDetail> list = orderServiceInf.detail(id);
        return Result.succ(list);
    }

    /**
     * 根据状态查询出订单
     * @param currentPage
     * @param pageSize
     * @param status
     * @return
     */
    @GetMapping("/searchStatus")
    public Result<PageListVo> searchStatus(@RequestParam String currentPage,
                                       @RequestParam String pageSize,
                                       @RequestParam String status){
        HashMap<String, String> map = new HashMap<>();
        map.put("currentPage",currentPage);
        map.put("pageSize",pageSize);
        map.put("status",status);
        PageListVo<Order> pageList = orderServiceInf.searchStatus(map);
        return Result.succ(pageList);
    }

    @GetMapping("/buy/{orderId}")
    public Result<?> buy(@PathVariable int orderId) {
        Order order = orderServiceInf.getById(String.valueOf(orderId));
//        String orderNo = IdUtil.getSnowflake().nextIdStr();
        String orderNo = order.getOrderNo();
        String payUrl = "http://localhost:9001/order/alipay/pay?subject=" + order.getUserName()+"的订单" + "&traceNo=" + orderNo + "&totalAmount=" + order.getTotalPrice();
        // 新建订单，扣减库存
        return Result.succ(payUrl);
    }

    @GetMapping("getById")
    public Result<Order> getById(@RequestParam String id){
        Order order = orderServiceInf.getById(id);
        return Result.succ(order);
    }
}
